package zhr.store.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import zhr.store.common.BaseContext;
import zhr.store.common.Result;
import zhr.store.domain.AddressBook;
import zhr.store.service.AddressBookService;

import java.util.List;

@RestController
@RequestMapping("/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    /**
     * 查询用户地址
     */
    @GetMapping("/list")
    public Result<List<AddressBook>> list(AddressBook addressBook) {
        //获取用户id
        addressBook.setUserId(BaseContext.getCurrentId());
        //构建查询语句
        LambdaQueryWrapper<AddressBook> queryWrapper = new LambdaQueryWrapper<>();
        if (addressBook.getUserId() != null) {
            queryWrapper.eq(AddressBook::getUserId, addressBook.getUserId());
            queryWrapper.orderByDesc(AddressBook::getUpdateTime);
        }
        //执行查询
        List<AddressBook> list = addressBookService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 设置默认地址
     */
    @PutMapping("default")
    public Result<String> setDefault(@RequestBody AddressBook addressBook) {
        //构建查询条件
        LambdaUpdateWrapper<AddressBook> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId());
        //清除默认地址
        updateWrapper.set(AddressBook::getIsDefault, 0);
        //执行修改
        addressBookService.update(updateWrapper);
        //更改当前默认地址
        addressBook.setIsDefault(1);
        addressBookService.updateById(addressBook);
        return Result.success("设置默认地址成功");
    }

    /**
     * 根据id查询地址
     */
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressBook(AddressBook addressBook) {
        //构建查询语句
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (addressBook.getId() != null) {
            addressBookLambdaQueryWrapper.eq(AddressBook::getId, addressBook.getId());
        }
        AddressBook one = addressBookService.getOne(addressBookLambdaQueryWrapper);
        return Result.success(one);
    }

    /**
     * 修改地址
     */
    @PutMapping
    public Result<String> update(@RequestBody AddressBook addressBook) {
        addressBookService.updateById(addressBook);
        return Result.success("修改成功");
    }

    /**
     * 删除地址
     */
    @DeleteMapping
    public Result<String> delete(Long ids) {
        //构建查询条件
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getId, ids);
        addressBookService.remove(addressBookLambdaQueryWrapper);
        return Result.success("删除成功");
    }

    /**
     * 新增地址
     *
     * @param addressBook
     * @return
     */
    @PostMapping
    public Result<AddressBook> save(@RequestBody AddressBook addressBook) {
        addressBook.setUserId(BaseContext.getCurrentId());
        addressBookService.save(addressBook);
        return Result.success(addressBook);
    }

    /**
     * 查询默认地址
     */
    @GetMapping("/default")
    public Result<AddressBook> getDefault() {
        //构建查询条件
        LambdaQueryWrapper<AddressBook> addressBookLambdaQueryWrapper = new LambdaQueryWrapper<>();
        addressBookLambdaQueryWrapper.eq(AddressBook::getUserId, BaseContext.getCurrentId())
                .eq(AddressBook::getIsDefault, 1);
        //查询
        AddressBook addressBook = addressBookService.getOne(addressBookLambdaQueryWrapper);
        if (addressBook == null) {
            return Result.error("未设置默认地址");
        }
        return Result.success(addressBook);
    }
}
